#!/usr/bin/env python
# encoding: utf-8
"""
Shovelhead
File: Panhead.py
Author: Myriam Boucher

Sound synthesis of a Panhead engine.

The engine is a two-cylinder, two-valve-per-cylinder, pushrod V-twin. The engine replaced
the Knucklehead engine in 1948 and was manufactured until 1965 when it was
replaced by the shovelhead. Source: <http://en.wikipedia.org/wiki/Harley-Davidson_Panhead_engine>
"""

from pyo import *
import random
import math
from LoopAdsr import *
from Gear import *
from Engine import *
import time

class Panhead(Engine):
    
    def __init__(self):
        Engine.__init__(self)  

        self.env5 = LoopAdsr(list=[(0,0),(250,1),(450,0.0006),(1200,.05),(900,0.005),(850,0)],
                dur=0.9, min=0.03, max=0.1, freq=[10,10.5])
        # effects on its rattling engine, noise
        self.r6 = Randi(min=0.19, max=.21, freq=1.00, mul=1, add=0)
        self.randomPitchRossler = Randi(min=0.32, max=.33, freq=1.00, mul=1, add=0)
        self.noise = Noise(.35)
        
        self.disto = Disto(  self.fm + self.fm2, drive=.45, slope=.52, mul=1, add=0)
        self.ross = Rossler(pitch=self.randomPitchRossler, chaos=0.98, stereo=False, mul=.6, add=0) 
        self.clipPanhead = Clip(self.ross, min=-1.00, max=1.00, mul=.5, add=0)
        self.pan2DPanhead = SPan(self.clipPanhead + self.eqPinkNoise + self.fm + self.disto  + self.filterSinwave
                                 + self.ross, outs=2, pan=0.50, mul=.5, add=0)
        self.setPan2D(self.pan2DPanhead)
        self.mixPanhead = Mix(self.pan2DPanhead, voices=2) 
        self.setMix(self.mixPanhead)
        
        # kickStart: crankshaft sound
        self.env4 = LoopAdsr(list=[(0,0),(250,1),(1000,0.0005),(1200,0.0005),(1350,0)],
                dur=0.09, min=0.01, max=0.03, freq=[10,10.2])
        self.pinkNoiseKS = PinkNoise(mul=self.env4.sig(), add=0)
        self.revKickStart = WGVerb(self.pinkNoiseKS, feedback=0.50, cutoff=5000, bal=0.32, mul=.38, add=0)
        self.mixNoStartingPanhead = Mix(self.revKickStart)
        self.setMixNoStarting(self.mixNoStartingPanhead)
        
    def changeSpeed(self, x):  
        # changes the sound of the engine when changing speed.
        self.fm.carrier += x*5
        self.fm.ratio += x/12
        self.fm.ind1 *= x*10
        self.fm.ind2 += x*1.6
        self.sinwave.freq += x*8
        self.fm2.carrier += x*10
        self.fm2.ratio += x/12
        self.fm2.ind1 *= x*18
        self.fm2.ind2 += x*2.5
    
    def playPanhead(self):
        # plays the audio objects.
        self.disto.play()
        self.noise.play()
        self.ross.play()
        self.pan2DPanhead.play()
        self.r6.play()
        self.randomPitchRossler.play()
        self.pinkNoiseKS.play()
        self.revKickStart.play()
        self.clipPanhead.play()
        
    
    def stopPanhead(self):
        # stop the audio objects.
        self.disto.stop()
        self.noise.stop()
        self.ross.stop()
        self.pan2DPanhead.stop()
        self.r6.stop()
        self.randomPitchRossler.stop()
        self.pinkNoiseKS.stop()
        self.revKickStart.stop()
        self.clipPanhead.stop()
        
        